package com.biboheart.breport.engine.engine;

import com.biboheart.breport.core.BrContext;
import com.biboheart.breport.dataset.BrReport;
import com.biboheart.breport.dataset.BrReportHandler;
import com.biboheart.breport.dataset.handler.BrReportHandlerDefault;
import com.biboheart.breport.engine.BrEngineAbstract;
import com.biboheart.breport.engine.BrParser;
import com.biboheart.breport.engine.compile.BrParserDefault;
import com.biboheart.breport.source.collector.BrCollectorSimple;
import com.biboheart.breport.template.BrTemplateHandler;
import com.biboheart.breport.template.BrTemplateStruct;
import com.biboheart.breport.template.handler.BrTemplateHandlerDefault;

import java.util.List;
import java.util.Map;

public class BrEngineSample extends BrEngineAbstract {
    // 数据集处理器
    private final BrReportHandler handler;
    // 模板处理器
    private final BrTemplateHandler templateHandler;

    public BrEngineSample(BrContext context) {
        super(context);
        handler = new BrReportHandlerDefault();
        templateHandler = new BrTemplateHandlerDefault();
    }

    @Override
    public BrReport generate() {
        BrCollectorSimple collector = new BrCollectorSimple();
        collector.setDataSource(context.getDataSource());
        List<Map<String, Object>> sourceList = collector.collection();
        BrTemplateStruct templateStruct = templateHandler.handle(context.getTemplate());
        BrParser parser = new BrParserDefault();
        List<Map<String, Object>> dataList = parser.parse(sourceList, templateStruct);
        return handler.handle(context, dataList, templateStruct);
    }
}
